package org.hamster.multidropbox.mainView.view
{
	import mx.collections.ArrayCollection;
	
	import org.hamster.common.puremvc.mediator.BaseMediator;
	import org.hamster.multidropbox.dropbox.event.AppDropboxEvent;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxFile;
	import org.hamster.multidropbox.main.services.AppFileBrowseService;
	import org.hamster.multidropbox.main.services.AppTipService;
	import org.hamster.multidropbox.main.services.StatusBarService;
	import org.hamster.multidropbox.main.utils.AppUtil;
	import org.hamster.multidropbox.mainView.event.AppFileEvent;
	import org.hamster.multidropbox.mainView.event.MainViewEvent;
	
	public class FileListViewMultiClientsMediator extends BaseMediator
	{
		public static const NAME:String = "org.hamster.multidropbox.mainView.view.FileListViewMultiClientsMediator";
		
		public var clientList:ArrayCollection;
		
		public function FileListViewMultiClientsMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			app.addEventListener(AppFileEvent.FILE_PATH_CHANGE, 	       filePathChangeHandler);
			app.addEventListener(MainViewEvent.FILE_ITEM_DOUBLE_CLICK,     fileItemDoubleClickHandler);
			app.addEventListener(MainViewEvent.FILE_ITEM_SHOW_DETIALS,     fileItemShowDetailsHandler);
			app.addEventListener(MainViewEvent.FILE_ITEM_HIDE_DETIALS,     fileItemHideDetailsHandler);
			app.addEventListener(MainViewEvent.UPLOAD_FILE_LOAD_COMPLETE,  uploadFileLoadCompleteHandler);
//			var body:Object = {
//				path : "",
//				clientList : clientList
//			};
//			this.sendNotification(AppDropboxEvent.DROPBOX_METADATA_LIST_REQUEST, body);
		}
		
		override public function onRemove():void
		{
			app.removeEventListener(AppFileEvent.FILE_PATH_CHANGE,			  filePathChangeHandler);
			app.removeEventListener(MainViewEvent.FILE_ITEM_DOUBLE_CLICK,     fileItemDoubleClickHandler);
			app.removeEventListener(MainViewEvent.FILE_ITEM_SHOW_DETIALS,     fileItemShowDetailsHandler);
			app.removeEventListener(MainViewEvent.FILE_ITEM_HIDE_DETIALS,     fileItemHideDetailsHandler);
			app.removeEventListener(MainViewEvent.UPLOAD_FILE_LOAD_COMPLETE,  uploadFileLoadCompleteHandler);
		}
		
		protected function filePathChangeHandler(event:AppFileEvent):void
		{
			var path:String = event.path;
			if (path == null) {
				path = "";
			}
			var clientList:ArrayCollection = clientList;
			var body:Object = {
				path : path,
				clientList : clientList
			};
			this.sendNotification(AppDropboxEvent.DROPBOX_METADATA_LIST_REQUEST, body);
		}
		
		protected function fileItemDoubleClickHandler(event:MainViewEvent):void
		{
			
		}
		
		protected function fileItemShowDetailsHandler(event:MainViewEvent):void
		{
			var f:AppDropboxFile = event.dropboxFile;
			StatusBarService.instance.info(
				AppUtil.getStrStatus("status.fileDetails", f.path, f.mimeType, f.modified));
		}
		
		protected function fileItemHideDetailsHandler(event:MainViewEvent):void
		{
			StatusBarService.instance.info("");
		}
		
		protected function uploadFileLoadCompleteHandler(event:MainViewEvent):void
		{
			var appFileBrowerService:AppFileBrowseService = event.data;
			
			var fileList:Array = appFileBrowerService.fileReferenceSuccessList;
			var clientList:ArrayCollection = this.clientList;
			var body:Object = {
				folderName : app.folderName,
				fileList   : new ArrayCollection(fileList),
				clientList : clientList
			};
			
			this.sendNotification(MainViewEvent.LINK_FOLDER_UPLOAD_POPUP_SHOW, body);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				AppDropboxEvent.DROPBOX_METADATA_LIST_RESULT,	
				AppDropboxEvent.DROPBOX_METADATA_LIST_FAULT,
			];
		}
		
		public function handleDropboxMetadataListResult(data:Object):void
		{
			var metadataList:ArrayCollection = data as ArrayCollection;
			this.app.fileList = metadataList;
		}
		
		public function handleDropboxMetadataListFault(data:Object):void
		{
			AppTipService.instance.error(AppUtil.getStrStatus("tip.loadMetadataListFailed.title"), AppUtil.getStrStatus("tip.loadMetadataListFailed.text", data));
		}
		
		public function get app():FileListView
		{
			return viewComponent as FileListView;
		}
	}
}